﻿using System;
using System.Data;
using System.Web.UI.WebControls;

public partial class CreditLine : System.Web.UI.Page
{
    ServerObject mServerObject = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        addNewDateBeginCalendarExtender.Format = Constants.Constants.ISODateBackwardDotsFormat;
        addNewDateEndCalendarExtender.Format = Constants.Constants.ISODateBackwardDotsFormat;
        editDateBeginCalendarExtender.Format = Constants.Constants.ISODateBackwardDotsFormat;
        editDateEndCalendarExtender.Format = Constants.Constants.ISODateBackwardDotsFormat;

        if (mServerObject == null) mServerObject = new ServerObject();
        if (Utils.AutentificatedUser)
        {
            if (!IsPostBack)
            {
                ShowPanel(allListPanel.ID);
            }
        }
        else
        {
            ShowPanel(string.Empty);
        }
    }

    private void ShowPanel(string panelName)
    {
        #region Hide panels

        emptyPanel.Visible = false;
        allListPanel.Visible = false;

        addNewEntryPanel.Visible = false;
        editEntryPanel.Visible = false;

        #endregion Hide panels

        try
        {
            #region Get Panel Name

            switch (panelName)
            {
                case "allListPanel":
                    allListPanel.Visible = true;
                    FillCreditLineListGridView();
                    break;

                case "addNewEntryPanel":
                    addNewEntryPanel.Visible = true;
                    {
                        DataTable addNewSourceTable = Utils.ModuleMain().GetCurrencyList();
                        Utils.FillSelector(addNewCurrencyDropDownList, addNewSourceTable, "name", "currencyID");
                    }

                    break;

                case "editEntryPanel":
                    editEntryPanel.Visible = true;
                    {
                        DataTable editSourceTable = Utils.ModuleMain().GetCurrencyList();
                        Utils.FillSelector(editCurrencyDropDownList, editSourceTable, "name", "currencyID");
                    }
                    break;

                default:
                    emptyPanel.Visible = true;
                    break;
            }

            #endregion Get Panel Name
        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Atentie! Eroare in sistem!", ex.Message);
        }
    }

    protected void FillCreditLineListGridView()
    {
        DataTable sourceDataTable = Utils.ModuleCredits().GetFundingLinesList();
        listGridView.DataSource = sourceDataTable;
        listGridView.DataBind();
    }

    private void ClearControlAddNewEntryPanel()
    {
        addNewNameTextBox.Text = string.Empty;
        addNewCodeTextBox.Text = string.Empty;
        addNewCurrencyDropDownList.SelectedIndex = -1;
        addNewDateBeginTextBox.Text = string.Empty;
        addNewDateEndTextBox.Text = string.Empty;
        addNewInitialAmountTextBox.Text = string.Empty;
        addNewRealRemainderTextBox.Text = string.Empty;
        addNewAnticipatedRemainderTextBox.Text = string.Empty;
    }

    protected void newButton_Click(object sender, EventArgs e)
    {
        ClearControlAddNewEntryPanel();
        ShowPanel(addNewEntryPanel.ID);
    }

    protected void addNewCancelButton_Click(object sender, EventArgs e)
    {
        ClearControlAddNewEntryPanel();
        ShowPanel(allListPanel.ID);
    }

    protected void addNewSaveButton_Click(object sender, EventArgs e) //todo: impliment id from selector
    {
        try
        {
            string name = addNewNameTextBox.Text.Trim();
            string code = addNewCodeTextBox.Text.Trim();

            int currencyID = 0;
            string currency = addNewCurrencyDropDownList.SelectedValue.ToString();
            int.TryParse(currency, out currencyID);

            string[] dateBeginString = addNewDateBeginTextBox.Text.Split('.');

            DateTime startDate = DateTime.MinValue;
            if (dateBeginString.Length == 3)
            {
                int day = 0;
                int.TryParse(dateBeginString[0], out day);

                int month = 0;
                int.TryParse(dateBeginString[1], out month);

                int year = 0;
                int.TryParse(dateBeginString[2], out year);

                if (day != 0 && month != 0 && year != 0)
                    startDate = new DateTime(year, month, day);
            }

            string[] dateEndString = addNewDateEndTextBox.Text.Split('.');

            DateTime endDate = DateTime.MinValue;
            if (dateEndString.Length == 3)
            {
                int day = 0;
                int.TryParse(dateEndString[0], out day);

                int month = 0;
                int.TryParse(dateEndString[1], out month);

                int year = 0;
                int.TryParse(dateEndString[2], out year);

                if (day != 0 && month != 0 && year != 0)
                    endDate = new DateTime(year, month, day);
            }

            decimal initialAmount = 0;
            decimal.TryParse(addNewInitialAmountTextBox.Text, out initialAmount);

            decimal realRemainder = 0;
            decimal.TryParse(addNewRealRemainderTextBox.Text, out realRemainder);

            decimal anticipatedRemainder = 0;
            decimal.TryParse(addNewAnticipatedRemainderTextBox.Text, out anticipatedRemainder);

            if (Utils.ModuleCredits().AddFundingLines(name, code, currencyID, startDate, endDate, initialAmount, realRemainder, anticipatedRemainder))
            {
                ClearControlAddNewEntryPanel();
                ShowPanel(allListPanel.ID);
            }
            else
            {
                Utils.InfoText(this, "Atentie", " { " + name + " } - nu a fost salvat. Verificati conectiunea cu internet ");
            }
        }
        catch (Exception ex)
        { Utils.InfoText(this, "Eroare in sistem!", ex.Message.ToString()); }
    }

    protected void listGridView_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        try
        {
            ShowPanel(editEntryPanel.ID);

            int index = e.NewEditIndex;            
            linesIDLabel.Text = listGridView.Rows[index].Cells[0].Text;
            editNameTextBox.Text = listGridView.Rows[index].Cells[1].Text;
            editCodeTextBox.Text = listGridView.Rows[index].Cells[2].Text;

            editDateBeginTextBox.Text = ((Label)listGridView.Rows[index].FindControl("DateBeginLabel")).Text;
            editDateEndTextBox.Text = ((Label)listGridView.Rows[index].FindControl("DateEndLabel")).Text;

            editInitialAmountTextBox.Text = listGridView.Rows[index].Cells[7].Text;
            editRealRemainderTextBox.Text = listGridView.Rows[index].Cells[8].Text;
            editAnticipatedRemainderTextBox.Text = listGridView.Rows[index].Cells[9].Text;
            editCurrencyDropDownList.SelectedValue = listGridView.Rows[index].Cells[3].Text;
        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Atentie! Eroare in sistem!", ex.Message);
        }
    }

    protected void editSaveButton_Click(object sender, EventArgs e)
    {
        try
        {
            string strTypeID = linesIDLabel.Text;
            int entryID = 0;
            int.TryParse(strTypeID, out entryID);

            string name = editNameTextBox.Text.Trim();
            string code = editCodeTextBox.Text.Trim();

            int currencyID = 0;
            string currency = editCurrencyDropDownList.SelectedValue.ToString();
            int.TryParse(currency, out currencyID);

            string[] dateBeginString = editDateBeginTextBox.Text.Split('.');
            DateTime startDate = DateTime.MinValue;
            if (dateBeginString.Length == 3)
            {
                int day = 0;
                int.TryParse(dateBeginString[0], out day);

                int month = 0;
                int.TryParse(dateBeginString[1], out month);

                int year = 0;
                int.TryParse(dateBeginString[2], out year);

                if (day != 0 && month != 0 && year != 0)
                    startDate = new DateTime(year, month, day);
            }


            string[] dateEndString = editDateEndTextBox.Text.Split('.');
            DateTime endDate = DateTime.MinValue;
            if (dateEndString.Length == 3)
            {
                int day = 0;
                int.TryParse(dateEndString[0], out day);

                int month = 0;
                int.TryParse(dateEndString[1], out month);

                int year = 0;
                int.TryParse(dateEndString[2], out year);

                if (day != 0 && month != 0 && year != 0)
                    endDate = new DateTime(year, month, day);
            }

            decimal initialAmount = 0;
            decimal.TryParse(editInitialAmountTextBox.Text, out initialAmount);

            decimal realRemainder = 0;
            decimal.TryParse(editRealRemainderTextBox.Text, out realRemainder);

            decimal anticipateRemainder = 0;
            decimal.TryParse(editAnticipatedRemainderTextBox.Text, out anticipateRemainder);

            if (Utils.ModuleCredits().UpdateFundingLines(entryID, name, code, currencyID, startDate, endDate, initialAmount, realRemainder, anticipateRemainder))
            {
                ClearControlEditPanel();
                ShowPanel(allListPanel.ID);
            }
            else
            {
                Utils.InfoText(this, "Atentie", " { " + name + " } - nu a fost salvat. Verificati conectiunea internetului ");
            }
        }
        catch (Exception ex)
        { Utils.InfoText(this, "Eroare in sistem!", ex.Message.ToString()); }
    }

    protected void listGridView_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
    {
        int index = e.RowIndex;

        try
        {
            string strTableID = listGridView.Rows[index].Cells[0].Text;
            int tableID = 0;
            int.TryParse(strTableID, out tableID);

            if (Utils.ModuleCredits().DeleteFundingLines(tableID))
            {
                ShowPanel(allListPanel.ID);
            }
            else
            {
                Utils.InfoText(this, "Atentie", " Stergerea liniei de credit nu a fost efectuata. Verificati conectiunea internetului ");
            }
        }
        catch (Exception ex)
        { Utils.InfoText(this, "Eroare in sistem!", ex.Message.ToString()); }
    }

    protected void editCancelButton_Click(object sender, EventArgs e)
    {
        ClearControlEditPanel();
        ShowPanel(allListPanel.ID);
    }

    protected void listGridView_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        listGridView.PageIndex = e.NewPageIndex;
        FillCreditLineListGridView();
    }

    private void ClearControlEditPanel()
    {
        editNameTextBox.Text = string.Empty;
        editCodeTextBox.Text = string.Empty;
        editCurrencyDropDownList.SelectedIndex = -1;
        editDateBeginTextBox.Text = string.Empty;
        editDateEndTextBox.Text = string.Empty;
        editInitialAmountTextBox.Text = string.Empty;
        editRealRemainderTextBox.Text = string.Empty;
        editAnticipatedRemainderTextBox.Text = string.Empty;
    }
}